package com.admin.education.controllers.api.v1;


import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.openId.OpenIdAuthorize;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.schools.SchoolsTransfer;
import com.admin.education.services.Interfaces.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/v1/schools")
@OpenIdAuthorize
public class SchoolController {

    @Autowired
    private ISchoolService schoolService;
    @GetMapping("")
    public Callable<IResponseMessage> getRoles(){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<List<SchoolsTransfer>> result = schoolService.querySchools();
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<List<SchoolsTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
